package cn.relx.org.service.impl;

import cn.relx.basic.query.BaseQuery;
import cn.relx.basic.service.impl.BaseServiceImpl;
import cn.relx.basic.util.MD5Utils;
import cn.relx.basic.util.PageList;
import cn.relx.basic.util.StrUtils;
import cn.relx.org.domain.Employee;
import cn.relx.org.mapper.EmployeeMapper;
import cn.relx.org.service.EmployeeService;
import cn.relx.system.mapper.RoleMapper;
import cn.relx.user.domain.Logininfo;
import cn.relx.user.service.ILogininfoService;
import cn.relx.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ILogininfoService logininfoService;

    @Override
    public List<Employee> empDepartmentId() {
        List<Employee> allDepts = employeeMapper.findAll();
        // stream
        Map<Long, Employee> parentMap = allDepts.stream().collect(Collectors.toMap(Employee::getId, e -> e));
        // 同理
//        Map<Long, Department> parentMap = new HashMap<>();
//        for (Department dept : allDepts) {
//            parentMap.put(dept.getId(),dept);
//        }
        List<Employee> result = new ArrayList<>();
        // 遍历
        for (Employee dept : allDepts) {
            if (null == dept.getDepartmentId()) {
                // 它就是最大的那个die
                result.add(dept);
            } else {
                // 当前的这个dept 是个儿子
                // 找爹 自己是个儿子 有爹的id
                Employee parent = parentMap.get(dept.getDepartmentId());
                List<Employee> children = parent.getChildren();
                children.add(dept);
            }
        }
        return result;
    }

    @Override
    public Employee selectByLogininfoId(Long id) {
        return employeeMapper.selectByLogininfoId(id);
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> selectByShopId(Long shopId) {
        return employeeMapper.selectByShopId(shopId);
    }

    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        List<Employee> list = employeeMapper.queryByPage(query);
        for (Employee employee : list) {
           List<Long> roleId = employeeMapper.findRoleById(employee.getId());
            employee.setRoles(roleId);
        }
        PageList<Employee> pageList = new PageList<>();
        pageList.setRows(list);
        return pageList;
    }

    @Override
    @Transactional
    public void add(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = employeeLogininfo(employee);
        logininfoService.add(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);
        initRelationTable(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = employeeLogininfo(employee);
        Employee byId = employeeMapper.findById(logininfo.getId());
        System.out.println(byId);
        logininfo.setId(byId.getLogininfoId());
        logininfoService.update(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
        initRelationTable(employee);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        Employee employee = employeeMapper.findById(id);
        logininfoService.delete(employee.getLogininfoId());
        employeeMapper.delete(id);
        employeeMapper.deleteRoleById(id);
    }

    /**
     * 此方法将用于employee表的数据拷贝到logininfo对象中
     * @param employee
     * @return
     */
    private Logininfo employeeLogininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    /**
     * 将此方法用于初始化employee对象的数据
     * @param employee
     */
    private void initEmployee(Employee employee){
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String md5 = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5);
    }

    public void initRelationTable(Employee employee){
        //根据用户id删除用户关联表的所有数据
        employeeMapper.removeEmp(employee.getId());
        //根据用户id、角色id集合新增关联表数据
        employeeMapper.addEmp(employee.getId(),employee.getRoles());
    }
}
